import ObjBase = require('./../sjg/kernel/obj/ObjBase');
import GroupObj = require('./../sjg/kernel/obj/GroupObj');
import Parser = require('./../sjg/kernel/base/Parser');
import CompoundPath = require('./../sjg/common/graph/CompoundPath');
import PointPath = require('./../sjg/common/graph/PointPath');
import Point = require('./../sjg/common/graph/Point');
import GlobalData = require('./../sjg/z/GlobalData');
var moment = require('moment');  // 日期格式化

export = CommonUtil;


class CommonUtil{

    // 'HH_mm_ss_SSS_'+随机数的字符串
    public static  genRandomStr() {
        var m = moment(new Date());
        var newDirName = m.format('HH_mm_ss_SSS_')+Math.floor(Math.random()*100000);
        return newDirName;
    }
    // 今天偏移i天得到'YYYY/MM/DD'的字符串
    public static genDayStr(i:number=0){
        var m = moment(new Date());
        return moment(m).add( i, 'days').format('YYYY/MM/DD');
    }

    // 深度复制［复制自身和子对象，递归］
    // 参数：obj 内核对象
    // 参数：ver 数据版本
    // 参数：g GlobalData
    public static deepCopy(obj: ObjBase, ver:number, g:GlobalData): ObjBase {
        var jsonStr: string = obj.toJson().replace(/\n/g,'\\n');// 处理Text对象的换行符
        var copy: ObjBase = Parser.fromUnknownJson(JSON.parse(jsonStr),ver, g);
        CommonUtil.deepSetId(copy,g);
        return copy;
    }
    // 为对象和子对象设置id，递归
    public static deepSetId(obj: ObjBase,g:GlobalData) {
        g.objManager().saveObject(obj,true);

        if (obj instanceof GroupObj) {
            var grpObj: GroupObj = <GroupObj>obj;
            for (var i in grpObj.children) {
                var childObj: ObjBase = grpObj.children[i];
                CommonUtil.deepSetId(childObj,g);
            }
        }
    }

    // 创建复合路径
    public static COMPOUNDPATH_RECT: number = 0;
    public static COMPOUNDPATH_CIRCLE: number = 1;
    public static COMPOUNDPATH_ROUND_RECT: number = 2;
    public static COMPOUNDPATH_SIX: number = 3;
    public static COMPOUNDPATH_EIGHT: number = 4;

    // 0矩形／
    public static g_data = [
        [
            [[0,0,8],[50,0,1],[50,50,1],[0,50,1]]
        ],
        [
            [[25,0,8],[39,0,3],[50,11,3],[50,25,3],     // bezier 0,右上的弧，下标： 0,1,2,3
                [50,39,3],[39,50,3],[25,50,3],          // bezier 1,右下的弧，下标： 4,5,6
                [11,50,3],[0,39,3],[0,25,3],            // bezier 2,左下的弧，下标： 7,8,9
                [0,11,3],[11,0,3],[25,0,3]]             // bezier 3,左上的弧，下标： 10,11,12
        ],
        [
            [[4.48,0,8],[45.52,0,1],[48,0,3],[50,2,3],[50,4.48,3],[50,45.52,1],[50,48,3],[48,50,3],[45.52,50,3],[4.48,50,1],[2,50,3],[0,48,3],[0,45.52,3],[0,4.48,1],[0,2,3],[2,0,3],[4.48,0,3]]
        ],
        [
            [[12.5,0,8],[37.5,0,1],[50,21.65,1],[37.5,43.3,1],[12.5,43.3,1],[0,21.65,1]]
        ],
        [
            [[14.65,0,8],[35.35,0,1],[50,14.64,1],[50,35.35,1],[35.35,50,1],[14.65,50,1],[0,35.35,1],[0,14.64,1]]
        ]
    ];
    public static createBasicCompoundPath(type: number): CompoundPath {
        var ret: CompoundPath = null;
        var data = CommonUtil.g_data[type];
        ret = new CompoundPath();

        var minx: number,maxx: number,miny: number,maxy: number;
        for (var i = 0;i < data.length;i++) {
            var path = data[i];
            var path0: PointPath = new PointPath();
            for (var j = 0;j < path.length;j++) {
                var point = path[j];
                var pt0: Point = new Point();
                pt0.x = point[0];
                pt0.y = point[1];
                pt0.type = point[2];
                path0.points.push(pt0);

                if (i == 0 && j == 0) {
                    minx = pt0.x;
                    maxx = pt0.x;
                    miny = pt0.y;
                    maxy = pt0.y;
                } else {
                    minx = minx < pt0.x ? minx : pt0.x;
                    maxx = maxx > pt0.x ? maxx : pt0.x;
                    miny = miny < pt0.y ? miny : pt0.y;
                    maxy = maxy > pt0.y ? maxy : pt0.y;
                }
            }
            ret.paths.push(path0);
        }

        ret.w = maxx - minx;
        ret.h = maxy - miny;

        return ret;
    }

    public static createCompoundPath(type: number,w: number,h: number): CompoundPath {
        var ret: CompoundPath = CommonUtil.createBasicCompoundPath(type);
        ret.adjustPosition(w / ret.w,h / ret.h,2);

        return ret;
    }

}


